package com.qzw.security.web.controller;

import com.qzw.common.api.base.PageReq;
import com.qzw.common.api.base.PageResp;
import com.qzw.common.web.model.Transfer;
import com.qzw.security.api.model.Role;
import com.qzw.security.api.model.User;
import com.qzw.security.api.service.IRoleService;
import com.qzw.security.api.service.IUserService;
import com.qzw.security.spring.config.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author ：quziwei
 * @date ：Created in 2020/6/9 3:40 下午
 * @description：用户管理
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "API通用用户管理")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @ApiOperation("保存用户")
    @PostMapping()
    public User save(User user) {
        String password = user.getPassword();
        if (!StringUtils.isEmpty(password)) {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        user = userService.update(user);
        return user;
    }


    @ApiOperation("查询用户")
    @GetMapping("/{id}")
    public User index(@PathVariable Integer id) {
        User user = userService.findById(id);
        return user;
    }

    @ApiOperation("分页查询")
    @GetMapping()
    public PageResp<User> list(PageReq pageReq) {
        PageResp<User> pageResp = userService.findAll(pageReq);
        return pageResp;
    }

    @ApiOperation("查看用户角色列表")
    @GetMapping("/role/{id}")
    public Transfer<Role, Integer> roleList(@PathVariable Integer id) {
        Transfer<Role, Integer> transfer = new Transfer<>();

        // 获取查询用户的角色
        List<Role> roles = roleService.findRolesByUserId(id);
        // 获取所有角色
        Role role = new Role();
        role.setCreateUserId(SecurityUtils.getUserId());
        List<Role> allRole = roleService.findAll(role);
        // 封装选中的角色id
        Set<Integer> checkedKeys = transfer.getCheckedKeys();
        for (Role r : roles) {
            checkedKeys.add(r.getId());
        }
        // 封装所有角色对象
        Set<Transfer<Role, Integer>.TransferData> data = transfer.getData();
        for (Role r : allRole) {
            Transfer<Role, Integer>.TransferData transferData = transfer.new TransferData();
            transferData.setKey(r.getId());
            transferData.setLabel(r.getName());
            transferData.setValue(r);
            data.add(transferData);
        }

        return transfer;
    }

    @PostMapping("/role")
    @ApiOperation("用户角色修改")
    public void roleEdit(Integer userId, @RequestParam(required = false) List<Integer> roleId) {
        userService.roleEdit(userId, roleId);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public void delete(@PathVariable Integer id) {
        userService.deleteById(id);
    }
}
